//
//  AYMAddCaseDiseaseController.m
//  ky
//
//  Created by guo3920591 on 16/5/6.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

#import "AYMAddCaseDiseaseController.h"
#import "AYMCaseCategoryViewController.h"
#import "AYMDiseaseDivViewController.h"
#import "UIImage+AYMExtension.h"
#import "AYMCaseManger.h"
@interface AYMAddCaseDiseaseController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,AYMCaseCategoryViewControllerDelegate,UITextViewDelegate>

@property(nonatomic,strong)UIButton *addPictureBtn;
@property(nonatomic,strong)UITextView *textView;
@property(nonatomic,strong)UIImage *imgPicture;
@property(nonatomic,assign)NSInteger countNum;
@property(nonatomic,copy)NSString *pathString;
@end

@implementation AYMAddCaseDiseaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    //布局界面
    [self setupUI];
}
/// 布局界面
-(void)setupUI{
    
    self.title = @"添加病例";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(composeClick)];
    
    self.view.backgroundColor = [UIColor colorWithRed:235.0/255 green:235.0/255 blue:237.0/255 alpha:1.0];
    
    //中间view布局
    UIView *caseDescriptionView = [[UIView alloc] init];
    
    caseDescriptionView.backgroundColor = [UIColor whiteColor];
    
    caseDescriptionView.frame = CGRectMake(0, 140,screenW, 300);
    
    [self.tableView addSubview:caseDescriptionView];
    
    UILabel *caseDescriptionLabel = [[UILabel alloc] init];
    
    caseDescriptionLabel.font = [UIFont systemFontOfSize:15];
    caseDescriptionLabel.text = @"病症描述:";
    
    [caseDescriptionView addSubview:caseDescriptionLabel];
    
    UILabel *addDescriptionLabel = [[UILabel alloc] init];
    addDescriptionLabel.font = [UIFont systemFontOfSize:14];
    addDescriptionLabel.text = @"添加疾病描述标签○";
    addDescriptionLabel.textColor = [UIColor colorWithRed:37.0/255 green:112/255.0 blue:230/255.0 alpha:1.0];
    [caseDescriptionView addSubview:addDescriptionLabel];
    
    UITextView * textView  = [[UITextView alloc] init];
    textView.text = self.models.descriptionName;
    self.textView = textView;
    textView.delegate = self;
    textView.backgroundColor = [UIColor colorWithRed:249.0/255 green:249.0/255 blue:249.0/255 alpha:1.0];
    [caseDescriptionView addSubview:textView];
    
    UILabel *caseMangerLabel =[[UILabel alloc] init];
    caseMangerLabel.font = [UIFont systemFontOfSize:16];
    caseMangerLabel.text = @"病例照片:";
    [caseDescriptionView addSubview:caseMangerLabel];
    
    
    UIButton *addButton = [[UIButton alloc] init];
    
    [addButton addTarget:self action:@selector(addCasePicture) forControlEvents:UIControlEventTouchUpInside];
    if (self.models.pathString==nil || [self.models.pathString isEqualToString:@""]) {
        [addButton setBackgroundImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateNormal];
    }else{
        [addButton setBackgroundImage:[UIImage imageWithContentsOfFile: [ NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",self.models.pathString]]] forState:UIControlStateNormal];
    }
    
    self.addPictureBtn = addButton;
    
    [caseDescriptionView addSubview:addButton];
    
    
    [caseDescriptionLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(caseDescriptionView.left).offset(10);
        make.top.equalTo(caseDescriptionView.top).offset(5);
        
    }];
    
    [addDescriptionLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(caseDescriptionLabel.centerY);
        make.right.equalTo(caseDescriptionView.right).offset(-15);
        
    }];
    
    [textView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(caseDescriptionLabel.bottom).offset(20);
        make.left.equalTo(caseDescriptionLabel.left);
        make.right.equalTo(caseDescriptionView.right);
        make.height.equalTo(100);
        
    }];
    
    [caseMangerLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(textView.bottom).offset(10);
        make.left.equalTo(textView.left);
    }];
    
    [addButton makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(caseMangerLabel.bottom).offset(20);
        make.left.equalTo(caseDescriptionView.left).offset(50);
        make.size.equalTo(CGSizeMake(70, 70));
        
    }];
    
}

#pragma mark - tableView数据源方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"caseCell"];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"caseCell"];
    }
    if (indexPath.row==0) {
        cell.textLabel.text = @"疾病类型:";
        cell.detailTextLabel.text = self.models.caseName;
        
    }else{
        cell.textLabel.text = @"疾病细分:";
        cell.detailTextLabel.text = self.models.baseName;
        
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

#pragma mark - tableView代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        AYMCaseCategoryViewController *caseCategoryVc = [[AYMCaseCategoryViewController alloc] init];
        
        caseCategoryVc.delegate = self;
        
        [self.navigationController pushViewController:caseCategoryVc animated:YES];
    }else{
        
        AYMDiseaseDivViewController *diseaseVc = [[AYMDiseaseDivViewController alloc] init];
        
        diseaseVc.diseasedivBlock = ^(NSString *diseaseStringName){
            
            [tableView cellForRowAtIndexPath:indexPath].detailTextLabel.text = diseaseStringName;
            
        };
        
        [self.navigationController pushViewController:diseaseVc animated:YES];
        
    }
    
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self.view endEditing:YES];
    
}

#pragma mark - 疾病选择代理
-(void)caseCategoryViewController:(AYMCaseCategoryViewController *)caseCategoryViewController caseStringName:(NSString *)caseStringName{
    
    NSIndexPath  *indexPath = self.tableView.indexPathForSelectedRow;
    
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    cell.detailTextLabel.text = caseStringName;
    
    
}

#pragma mark - 图片按钮点击事件
-(void)addCasePicture{
    
    UIImagePickerController *imagePickerVc = [[UIImagePickerController alloc] init];
    imagePickerVc.delegate = self;
    
    [self presentViewController:imagePickerVc animated:YES completion:nil];
    
    
}

#pragma mark - 图片选择代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *imagePicture = info[@"UIImagePickerControllerOriginalImage"];
    UIImage *scaleImagePicture =[imagePicture scaleToWidth:120];
    
    self.imgPicture = scaleImagePicture;
    
    NSString *path_sandox = NSHomeDirectory();
    
    if (self.btn.tag == 100) {
        path_sandox  =[ path_sandox stringByAppendingString:[NSString stringWithFormat:@"/Documents/sss%zd.png",self.countNum1]];
        
        self.pathString = [NSString stringWithFormat:@"sss%zd.png",self.countNum1];
    }else{
        path_sandox = [path_sandox stringByAppendingString:[NSString stringWithFormat:@"/Documents/aaa%zd.png",self.countNum1]];
        self.pathString = [NSString stringWithFormat:@"aaa%zd.png",self.countNum1];
    }
    
    
    [UIImagePNGRepresentation(scaleImagePicture) writeToFile:path_sandox atomically:YES];
    
    [self.addPictureBtn setBackgroundImage:scaleImagePicture forState:UIControlStateNormal];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark - textview的代理方法
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        
        return NO;
    }
    
    return YES;
}

#pragma mark - 提交点击事件

-(void)composeClick{
    
    NSIndexPath *indxePath1 = [ NSIndexPath indexPathForRow:0 inSection:0];
    NSIndexPath *indxePath2 = [ NSIndexPath indexPathForRow:1 inSection:0];
    NSString *caseCategoryName=[self.tableView cellForRowAtIndexPath:indxePath1].detailTextLabel.text;
    NSString *caseDeaseDivName=[self.tableView cellForRowAtIndexPath:indxePath2].detailTextLabel.text;
    NSString *textViewName = self.textView.text;
    
    if (self.btn.tag == 100&& ![self.caseNameCatrgory isEqualToString:@""]) {
        AYMCaseManger *model = [[AYMCaseManger alloc] init];
        if (self.pathString) {
            
            model.pathString = self.pathString;
        }
        
        model.caseName = caseCategoryName;
        model.baseName = caseDeaseDivName;
        model.descriptionName = textViewName;
        
        
        if (!caseCategoryName ) {
            
            [SVProgressHUD showErrorWithStatus:@"请填写疾病描述"];
        }else{
            
            if (self.addCaseDiseaseBlock) {
                self.countNum++;
                self.addCaseDiseaseBlock(model,self.countNum);
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else{
        
        if (self.pathString) {
            self.models.pathString = self.pathString;
        }
        self.models.caseName = caseCategoryName;
        self.models.baseName = caseDeaseDivName;
        self.models.descriptionName = textViewName;
        
        if ([caseCategoryName isEqualToString:@""]) {
            
            [SVProgressHUD showErrorWithStatus:@"请填写疾病描述"];
        }else{
            
            if (self.caseUpDataModel) {
                
                self.caseUpDataModel();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    
}

@end
